package ahblue.cn.tanzi.ui.circle;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.data.source.ApiCallback;
import ahblue.cn.tanzi.data.source.RemoteRepository;
import ahblue.cn.tanzi.model.circle.Circle;
import ahblue.cn.tanzi.model.user.LoginResultModel;
import ahblue.cn.tanzi.ui.base.BaseActivity;
import ahblue.cn.tanzi.util.GlideApp;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CircleMyselfActivity extends BaseActivity {

    @BindView(R.id.textViewTitle)
    TextView mTextViewTitle;

    @BindView(R.id.recyclerViewCircle)
    RecyclerView mRecyclerViewCircle;

    private List<Circle> mCircle4Myself;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_circle_myself);

        ButterKnife.bind(this);

        initData();
        initView();
    }

    private void initData() {
        LoginResultModel loginResultModel = LoginResultModel.getUserModel(this, LoginResultModel.class);

        RemoteRepository repo = RemoteRepository.getsInstance();// 获取我加入的圈子
        repo.getBigShotCommentMyCircle(loginResultModel.token, mCallbackGetBigShotCommentMyCircle);
    }

    private void initView() {
        mTextViewTitle.setText("我的圈子");
    }

    // 单击返回按钮
    @OnClick(R.id.imageButtonLeft)
    void onClickLeftButton() {
        finish();
    }



    /**
     * 我的圈子接口回调
     */
    ApiCallback<List<Circle>> mCallbackGetBigShotCommentMyCircle = new ApiCallback<List<Circle>>() {
        @Override
        public void onSuccess(List<Circle> data) {
            mCircle4Myself = data;

            CircleAdapter adapter = new CircleAdapter(mCircle4Myself);
            adapter.setOnItemClickListener(mOnCircleClickListener);

            mRecyclerViewCircle.setLayoutManager(new LinearLayoutManager(CircleMyselfActivity.this));
            mRecyclerViewCircle.setAdapter(adapter);
        }

        @Override
        public void onFailed(String errorMsg) {

        }
    };



    // 单击圈子条目时的处理
    private BaseQuickAdapter.OnItemClickListener mOnCircleClickListener = new BaseQuickAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            Circle circle = mCircle4Myself.get(position);
            Intent intent = new Intent(CircleMyselfActivity.this, CircleDetailActivity.class);
            intent.putExtra(CircleDetailActivity.ARG_CIRCLE_ID, circle.id);

            CircleMyselfActivity.this.startActivity(intent);
        }
    };

    /**
     * 列表的适配器
     */
    class CircleAdapter extends BaseQuickAdapter<Circle, BaseViewHolder> {

        CircleAdapter(@Nullable List<Circle> data) {
            super(R.layout.item_my_circle, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Circle item) {
            ImageView iv = helper.getView(R.id.img_head);
            GlideApp.with(iv).load(item.head_portrait)
                    .placeholder(R.drawable.user_default_avatar)
                    .error(R.drawable.user_default_avatar)
                    .into(iv);

            helper.setText(R.id.tv_name, item.circle_name)
                    .setText(R.id.tv_intro, item.simple_introduce)
                    .setText(R.id.tv_member_count, Integer.toString(item.member_count))
                    .setText(R.id.tv_info_count, Integer.toString(item.dynamic_count))
                    .setText(R.id.tv_course_count, Integer.toString(item.curriculum_count));
        }
    }
}
